package com.apes.fico.finance.service;

import com.apes.fico.finance.model.costcollect.TypeCost;
import com.apes.fico.finance.respository.TypeCostRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.finance.model.IncomeType;
import com.apes.scm.masterdata.finance.repository.IncomeTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yiny
 */
@Service("typeCostService")
public class TypeCostService extends DomainService{

    @Autowired
    private TypeCostRepository typeCostRepository;


    public TypeCost save(SimpleRequest request){
        TypeCost typeCost = request.getO(TypeCost.class);
        return typeCostRepository.saveAndFlush(typeCost);
    }
    public TypeCost update(SimpleRequest request) {
        TypeCost typeCost = request.getO(TypeCost.class);
        typeCost.setUpdateDate(new Date());
        return typeCostRepository.saveAndFlush(typeCost);
    }

    public void delete(SimpleRequest request) throws Exception  {
        String id = request.get("id");
        typeCostRepository.delete(id);
    }

    public Page<TypeCost> findAll(SimpleRequest request) {
        List<Map> list = request.get("condition");
        return typeCostRepository.findAll(JpaDsl.toCriteria(list), PageRequest.of(request.get("page"), request.get("size")));
    }
}
